#include <bits/stdc++.h>
using namespace std;
#define int long long

void solve(){
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<vector<int>> memo(n + 1, vector<int>(32, -1));
    auto dfs = [&](auto &&dfs, int u, int x) ->int{
        if(u == n) return 0;
        int& res = memo[u][x];
        if(res != -1) return res;
        res = 0;
        if(x >= 30) {
            return 0;
        }else{
            int t1 = (a[u] >> x) - k;
            int t2 = a[u] >> (x + 1);
            res = max(dfs(dfs, u + 1, x) + t1, dfs(dfs, u + 1, x + 1) + t2);
        }
        return res;
    };  

    cout << dfs(dfs, 0, 0) << endl;
}

signed main(){
    int t;
    cin >> t;
    while (t--){
        solve();
    }
    return 0;
}